package com.web.base.core.provider;

import com.blue.core.dict.SeqType;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.core.exception.SequenceException;
import com.web.base.core.facade.SequenceFacade;
import com.web.base.core.message.SequenceRequest;
import com.web.base.core.message.SequenceResponse;
import com.web.base.core.model.Sequence;
import com.web.base.core.service.SequenceService;
import com.web.base.core.util.SequenceConvertor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 序列接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月19日
 */
@Component("coreSequenceProvider")
public class SequenceProvider implements SequenceFacade
{
	@Autowired
	private SequenceService sequenceService;
	
	public SequenceProvider()
	{
	}

	@Override
	public SequenceResponse add(SequenceRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Sequence sequence = SequenceConvertor.toSequence(request);
		sequence = sequenceService.save(sequence);
		SequenceResponse response = SequenceConvertor.toResponse(sequence);
		return response;
	}

	@Override
	public SequenceResponse update(SequenceRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Sequence sequence = SequenceConvertor.toSequence(request);
		sequence = sequenceService.save(sequence);
		SequenceResponse response = SequenceConvertor.toResponse(sequence);
		return response;
	}

	@Override
	public SequenceResponse get(Integer id)
	{
		if (id == null)
			throw new SequenceException("ID不能为空");
			
		Sequence sequence = sequenceService.get(id);
		SequenceResponse response = SequenceConvertor.toResponse(sequence);
		return response;
	}

	@Override
	public Page<SequenceResponse> listPage(SequenceRequest request, int start, int size)
	{
		Sequence sequence = SequenceConvertor.toSequence(request);
		Page<Sequence> page = new Page<>(size);
		page.setCurPage(start);
		page = sequenceService.list(sequence, page);
		Page<SequenceResponse> pageResponse = SequenceConvertor.toResponse(page);
		return pageResponse;
	}

	@Override
	public String nextVal(String code)
	{
		if (code == null || code.isEmpty())
			throw new SequenceException("代码不能为空");

		Sequence seq = sequenceService.getByCode(code);

		StringBuilder val = new StringBuilder(seq.getLength() + 20);
		if (seq.getPrefix() != null && !seq.getPrefix().equals("")) // 前缀
			val.append(seq.getPrefix());
		if (seq.getType() == SeqType.NORMAL) // 普通
		{
			val.append(StringUtils.leftPad(String.valueOf(seq.getVal()), seq.getLength(), "0"));
		}
		else // 时间
		{
			SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
			val.append(f.format(seq.getDate()));
			val.append(StringUtils.leftPad(String.valueOf(seq.getVal()), seq.getLength() - 8, "0"));
		}
		if (seq.getSuffix() != null && !seq.getSuffix().equals("")) // 后缀
			val.append(seq.getSuffix());
		return val.toString();
	}

}
